package cn.iocoder.yudao.module.yyzx.controller.admin.retreat;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.retreat.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.retreat.RetreatDO;
import cn.iocoder.yudao.module.yyzx.service.retreat.RetreatService;

@Tag(name = "管理后台 - 退住登记")
@RestController
@RequestMapping("/yyzx/retreat")
@Validated
public class RetreatController {

    @Resource
    private RetreatService retreatService;

    @PostMapping("/create")
    @Operation(summary = "创建退住登记")
    @PreAuthorize("@ss.hasPermission('yyzx:retreat:create')")
    public CommonResult<Integer> createRetreat(@Valid @RequestBody RetreatSaveReqVO createReqVO) {
        return success(retreatService.createRetreat(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新退住登记")
    @PreAuthorize("@ss.hasPermission('yyzx:retreat:update')")
    public CommonResult<Boolean> updateRetreat(@Valid @RequestBody RetreatSaveReqVO updateReqVO) {
        retreatService.updateRetreat(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除退住登记")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:retreat:delete')")
    public CommonResult<Boolean> deleteRetreat(@RequestParam("id") Integer id) {
        retreatService.deleteRetreat(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得退住登记")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:retreat:query')")
    public CommonResult<RetreatRespVO> getRetreat(@RequestParam("id") Integer id) {
        RetreatDO retreat = retreatService.getRetreat(id);
        return success(BeanUtils.toBean(retreat, RetreatRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得退住登记分页")
    @PreAuthorize("@ss.hasPermission('yyzx:retreat:query')")
    public CommonResult<PageResult<RetreatRespVO>> getRetreatPage(@Valid RetreatPageReqVO pageReqVO) {
        PageResult<RetreatDO> pageResult = retreatService.getRetreatPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RetreatRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出退住登记 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:retreat:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRetreatExcel(@Valid RetreatPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RetreatDO> list = retreatService.getRetreatPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "退住登记.xls", "数据", RetreatRespVO.class,
                        BeanUtils.toBean(list, RetreatRespVO.class));
    }

}